﻿using ITSM.Model.Entity;
using ITSM.Model.Model;
using ITSM.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ITSM.Controllers.DemandType
{
    [Route("api/[controller]")]
    [ApiController]
    public class DemandTypeController : ControllerBase
    {
        private readonly IDemandType _demandType;
        private readonly IDemandTypeLink _demandTypeLink;
        public DemandTypeController(IDemandType demandType, IDemandTypeLink demandTypeLink)
        {
            _demandType = demandType;
            _demandTypeLink = demandTypeLink;
        }
        [HttpGet("/api/GetDemandTypes")]
        public IActionResult GetDemandTypes(string RYD_Company) {
            if (string.IsNullOrWhiteSpace(RYD_Company))
            {
                return NotFound();
            }
            return Ok(_demandType.GetDemandTypes(RYD_Company).Select(res => new M_DemandType { RYD_DemandTypeName = res.RYD_DemandTypeName, RYD_DemandTypeNo = res.RYD_DemandTypeNo }));
        }

        [HttpGet]
        public IActionResult QueryTypeList(string RYD_Company) {
            if (string.IsNullOrWhiteSpace(RYD_Company))
            {
                return NotFound();
            }
            return Ok(_demandType.QueryDemandType(RYD_Company).Select(res => new M_DemandType { RYD_DemandTypeName = res.RYD_DemandTypeName, RYD_DemandTypeNo = res.RYD_DemandTypeNo }));
        }
        [HttpPost]
        public IActionResult AddDemandType(M_DemandType type)
        {
            if (string.IsNullOrWhiteSpace(type.RYD_Company) || string.IsNullOrWhiteSpace(type.RYD_UserId)||string.IsNullOrWhiteSpace(type.RYD_DemandTypeName))
            {
                return NotFound();
            }
            return Ok(_demandType.AddDemandType(type));
        }
        [HttpPut]
        public IActionResult QueryUserTypeLink(RYD_DemandTypeLink typeLink)
        {
            if (string.IsNullOrWhiteSpace(typeLink.RYD_Company) || string.IsNullOrWhiteSpace(typeLink.RYD_UserId))
            {
                return NotFound();
            }
            return Ok(_demandTypeLink.GetUserTypeLink(typeLink));
        }

        [HttpDelete]
        public IActionResult DeleteType(RYD_DemandTypeLink typeLink)
        {
            if (string.IsNullOrWhiteSpace(typeLink.RYD_Company) || string.IsNullOrWhiteSpace(typeLink.RYD_UserId))
            {
                return NotFound();
            }
            return Ok(_demandType.DeleteDemandType(typeLink));
        }

    }
}
